class Solution(object):
    def maxTurbulenceSize(self, arr):
        last0 = 1
        last1 = 1
        n = len(arr)
        max_len = 1
        for i in range(1, n):
            if arr[i] > arr[i - 1]:
                last0 = last1 + 1
                last1 = 1
            if arr[i] < arr[i - 1]:
                last1 = last0 + 1
                last0 = 1
            if arr[i] == arr[i - 1]:
                last0 = 1
                last1 = 1
            max_len = max(max_len, last0, last1)
        return max_len



data = Solution()
arr = [9, 4, 2, 10, 7, 8, 8, 1, 9]
print(data.maxTurbulenceSize(arr))
